package com.realtyreg.webview.controller;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.realtyreg.domain.entity.message.Message;
import com.realtyreg.service.MessageService;
import com.realtyreg.service.util.SecurityUtil;
import com.realtyreg.webview.message.MessageSaver;
import com.realtyreg.webview.message.MessageType;

import javax.validation.Valid;
import java.util.List;
import java.util.Locale;

/**
 * Created by Petro 24.03.13 0:34
 */
@Controller
@RequestMapping("/msg")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private MessageSaver messageSaver;

    //список входящих
    @RequestMapping(method = RequestMethod.GET)
    public String listInbox(Model model) {
        List<Message> inboxList = messageService.getInboxList();
        model.addAttribute("list", inboxList);
        return "messageInboxList";
    }

    //список отправленных
    @RequestMapping(params = "sent", method = RequestMethod.GET)
    public String listSent(Model model) {
        List<Message> sentList = messageService.getSentList();
        model.addAttribute("list", sentList);
        return "messageSentList";
    }

    //просмотр сообщения
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, Model model) {
        Message message = messageService.findById(id);
        model.addAttribute(message);
        return "messageView";
    }

    //отправка сообщения GET
    @RequestMapping(params = "form", method = RequestMethod.GET)
    public String sendForm(Model model) {
        model.addAttribute(new Message());
        return "messageEdit";
    }

    //отправка сообщения POST
    @RequestMapping(params = "form", method = RequestMethod.POST)
    public String send(@Valid @ModelAttribute Message message, BindingResult result, SessionStatus status, Model model, RedirectAttributes redirectAttributes, Locale locale) {
        if (result.hasErrors()) {
            return "messageEdit";
        }
        message.setSender(SecurityUtil.getUser());
        message.setSendDate(DateTime.now());
        messageService.send(message);
        status.setComplete();
        messageSaver.saveSessionMessage(redirectAttributes, MessageType.SUCCESS, "message.send.success", locale);
        return "redirect:/msg?sent";
    }

    //удаление сообщения
    @RequestMapping(value = "/{id}", params = "?delete", method = RequestMethod.GET)
    public String delete(@PathVariable Long id) {
        messageService.delete(id);
        return "redirect:/msg";
    }
}
